package com.bruceewu.galleryphoto.utis;

import android.graphics.BitmapFactory;
import android.media.ExifInterface;
import android.text.TextUtils;
import android.util.Pair;

import com.bruceewu.galleryphoto.bean.ImageBean;

import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class FileUtils {
    private final static List<String> filter = new ArrayList<>();

    static {
        filter.add("jpg");
        filter.add("gif");
        filter.add("png");
        filter.add("jpeg");
    }

    public static List<ImageBean> files(String path) {
        List<ImageBean> items = new ArrayList<>();
        if (!TextUtils.isEmpty(path)) {
            File dir = new File(path);
            if (dir.isFile()) {
                if (isImg(dir)) {
                    items.add(ImageBean.newInstance(dir));
                }
            } else {//递归
                File[] files = dir.listFiles();
                if (files != null) {
                    for (File file : files) {
                        items.addAll(files(file.getAbsolutePath()));
                    }
                }
            }
        }
        return items;
    }

    //单位 M
    public static String size(File file) {
        if (file != null && file.exists()) {
            long size = file.length();
            DecimalFormat df = new DecimalFormat("0.## ");
            Float si = size / 1024f / 1024f;
            return df.format(si) + "M";
        } else {
            return "0Kb";
        }
    }

    private static boolean isImg(File file) {
        String tag = ".";
        String path = file.getAbsolutePath();
        if (!TextUtils.isEmpty(path) && path.contains(tag)) {
            String name = file.getName();
            if (!TextUtils.isEmpty(name) && !name.startsWith(tag)) {//有的是.开头的隐藏文件，是无效的
                int index = path.lastIndexOf(tag);
                if (index >= 0) {
                    String fileType = path.substring(index + 1);
                    for (String fil : filter) {
                        if (fileType.equalsIgnoreCase(fil)) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }

    //根据屏幕比例缩放图片,保证图片居中不裁切
    public static Pair<Integer, Integer> scaledSize(String path) {
        Pair<Integer, Integer> pair = size(path);
        if (pair != null) {
            return ScreenUtils.scaleSize(pair.first, pair.second);
        }
        return null;
    }

    private static Pair<Integer, Integer> size(String path) {
        if (!TextUtils.isEmpty(path)) {
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, opts);
            int width = opts.outWidth;
            int height = opts.outHeight;
            int degree = FileUtils.getBitmapDegree(path);
            if (degree == 90 || degree == 270) {//图片有角度
                return new Pair<>(height, width);
            } else {
                return new Pair<>(width, height);
            }
        }
        return null;
    }

    private static int getBitmapDegree(String path) {
        int degree = 0;
        try {
            // 从指定路径下读取图片，并获取其EXIF信息
            ExifInterface exifInterface = new ExifInterface(path);
            // 获取图片的旋转信息
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }
}
